﻿using CodeGenerator.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator.Models
{
    /// <summary>
    /// 
    /// </summary>
    public class GeneratorModel
    {

        /// <summary>
        /// 命名空间
        /// </summary>
        public string NameSpace { get; set; }

        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; set; }

        /// <summary>
        /// 表描述
        /// </summary>
        public string TableDesc { get; set; }

        /// <summary>
        /// 字段属性
        /// </summary>
        public List<GeneratorFieldModel> FieldsModels { get; set; }

        /// <summary>
        /// 驼峰表名
        /// </summary>
        public string TableNameHump
        {
            get
            {
                return TableName.LineToHump().FirstCharToUpper();
            }
        }
        public string TableNameRemoveT
        {
            get
            {
                return (TableNameHump.StartsWith("T") ? TableNameHump.Substring(1) : TableNameHump).ToLower();
            }
        }
        /// <summary>
        /// 生成的属性
        /// </summary>
        public string Fields
        {
            get
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (var field in FieldsModels)
                {
                    stringBuilder.AppendLine(field.FieldDescText);
                    stringBuilder.AppendLine(field.PropertyText);
                }
                return stringBuilder.ToString();
            }
        }

    }

    /// <summary>
    /// 
    /// </summary>
    public class GeneratorFieldModel
    {
        /// <summary>
        /// 插入时忽略的字段
        /// </summary>
        public string[] IgnoreInserFileds { get; set; }

        /// <summary>
        /// 字段备注
        /// </summary>
        public string FieldDesc { get; set; }

        /// <summary>
        /// 修饰符
        /// </summary>
        public string ModifyName { get; set; } = "public";

        /// <summary>
        /// 字段类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 字段类型
        /// </summary>
        public Type SystemType { get; set; }

        /// <summary>
        /// 字段名称
        /// </summary>
        public string FieldName { get; set; }

        /// <summary>
        /// 字段属性
        /// </summary>
        public string Property { get; set; } = "{ get; set; }";

        /// <summary>
        /// 默认值
        /// </summary>
        public string DefaultValue { get; set; }

        /// <summary>
        /// 是否主键
        /// </summary>
        public bool IsPrimaryKey { get; set; }

        /// <summary>
        /// 是否自增长
        /// </summary>
        public bool IsIdentity { get; set; }

        /// <summary>
        /// 是否可为Null
        /// </summary>
        public bool IsNullable { get; set; }

        /// <summary>
        /// 字段长度
        /// </summary>
        public int MaxLength { get; set; }

        /// <summary>
        /// 是否添加SqlSugar特性
        /// </summary>
        public bool IsAddAttribute { get; set; } = true;

        readonly string startDesc = "\t\t/// <summary>";
        readonly string endDesc = "\t\t/// </summary>";

        /// <summary>
        /// 驼峰字段名
        /// </summary>
        public string FieldNameHump { get { return FieldName; } }

        /// <summary>
        /// 字段特性
        /// </summary>
        public string AttributeText
        {
            get
            {
                List<string> attrs = new List<string>();
                attrs.Add($"\t\t[SugarColumn(ColumnName = \"{FieldName}\"");
                if (IsPrimaryKey)
                    attrs.Add("IsPrimaryKey = true");
                if (IsIdentity)
                    attrs.Add("IsIdentity = true");
                if (!string.IsNullOrEmpty(DefaultValue) && Type.ToLower() != "datetime")
                    attrs.Add($"DefaultValue = \"{DefaultValue}\"");
                if ((Type.ToLower() == "datetime" && DefaultValue == "CURRENT_TIMESTAMP") || IgnoreInserFileds.Any(s => s.ToLower() == FieldName.ToLower()))
                {
                    attrs.Add("IsOnlyIgnoreInsert = true");
                }
                return string.Join(", ", attrs) + ")]";
            }

        }

        /// <summary>
        /// 注释值
        /// </summary>
        public string FieldDescText
        {
            get
            {
                List<string> list = new List<string>();
                list.Add(startDesc);
                list.Add($"\t\t///{FieldDesc}");
                if (!string.IsNullOrEmpty(DefaultValue))
                {
                    list.Add($"\t\t///默认值:{DefaultValue}");
                }
                list.Add(endDesc);

                if (IsAddAttribute)
                {
                    list.Add(AttributeText);
                }

                return "\r\n" + string.Join("\r\n", list);
            }
        }

        /// <summary>
        /// 属性值
        /// </summary>
        public string PropertyText
        {
            get
            {
                string isNull = (IsNullable && Type != "byte[]") ? "?" : "";
                return "\t\t" + string.Join(" ", ModifyName, Type + isNull, FieldNameHump, Property);
            }
        }
    }
}
